package com.jerei.plugin.control;

import com.jerei.plugin.entity.WcmCommonGroup;
import com.jerei.plugin.service.IWcmCommonGroupService;
import com.jerei.utils.UtilsCommon;
import com.jerei.utils.UtilsCondition;
import com.jerei.utils.spring.PageBean;
import com.jerei.web.plugin.controller.PluginController;
import com.jerei.web.utils.UtilsUI;
import com.jerei.web.utils.UtilsWeb;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import ro.fortsoft.pf4j.Extension;

/**
 * Created by sunfeihu on 2016/12/2.
 */
@Extension
public class GroupControl extends PluginController {

    @Autowired
    private IWcmCommonGroupService iWcmCommonGroupService;


    @RequiresPermissions({"common-group:save", "common-group:update"})
    public String save() {
        WcmCommonGroup entity = this.wrapper(WcmCommonGroup.class);
        if (entity.getId() != null && entity.getId() > 0L) {
            iWcmCommonGroupService.update(entity);
        } else {
            iWcmCommonGroupService.save(entity);
        }
        return UtilsUI.ok();
    }

    @RequiresPermissions({"common-group:remove"})
    public String delete() {
        String[] ids = this.getRequest().getParameterValues("ids");
        if (null != ids && ids.length > 0) {
            for (int i = 0; i < ids.length; i++) {
                iWcmCommonGroupService.delete(UtilsCommon.getLong(ids[i]));
            }
            return UtilsUI.ok();
        }
        return UtilsUI.fail();
    }

    @RequiresPermissions({"common-group:save", "common-group:update", "common-group:view"})
    public void edit() {
        Long id = UtilsWeb.getLong(this.getRequest(), "id");
        WcmCommonGroup entity = null;
        if (null != id) {
            entity = iWcmCommonGroupService.find(id);
        } else {
            entity = new WcmCommonGroup();
        }
        this.getModel().addAttribute("obj", entity);
    }

    @RequiresPermissions({"common-group:view"})
    public String page() {
        Integer nowPage = UtilsWeb.getInteger(this.getRequest(), "page");
        Integer pageSize = UtilsWeb.getInteger(this.getRequest(), "rows");
        PageBean<WcmCommonGroup> pageBean = iWcmCommonGroupService.page(nowPage, pageSize, UtilsCondition.getConditionEntity(this.getRequest().getParameterMap()));
        return UtilsUI.datagrid(pageBean);
    }

}